package weeny.pre.io.parser;

import prefuse.data.parser.DataParseException;
import prefuse.data.parser.DataParser;

import static prefuse.data.parser.CollectionParser.csv2List;
import static prefuse.data.parser.CollectionParser.string2Csv;

public class StringArrayParser implements DataParser{
	public boolean canParse(String text) {
		try {
			csv2List(text);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	public boolean compatible(Class type) {
		return type == getType();
	}
	public String format(Object value) {
		if (!(value instanceof String[])) {
			throw new IllegalArgumentException("value should be String[]");
		}
		return string2Csv((String[]) value);
	}
	public Class getType() {
		return String[].class;
	}
	public Object parse(String text) throws DataParseException {
		return csv2List(text).toArray(new String[0]);
	}
}
